/*
 * @Descripttion: 
 * @version: 
 * @Author: Ansel
 * @Date: 2020-03-24 11:08:58
 * @LastEditors: Ansel
 * @LastEditTime: 2020-03-24 12:07:10
 */
// var arr = [1,2,[3,[4,5]]].flat(Infinity) //[1, 2, 3, 4, 5]

var flatten = function(obj){
    var result = {};

    function recurse(src, prop) {
        var toString = Object.prototype.toString;
        if (toString.call(src) == '[object Object]') {
            var isEmpty = true;
            for (var p in src) {
                isEmpty = false;
                recurse(src[p], prop ? prop + '.' + p : p)
            }
            if (isEmpty && prop) {
                result[prop] = {};
            }
        } else if (toString.call(src) == '[object Array]') {
            var len = src.length;
            if (len > 0) {
                src.forEach(function (item, index) {
                    recurse(item, prop ? prop + '.[' + index + ']' : index);
                })
            } else {
                result[prop] = [];
            }
        } else {
            result[prop] = src;
        }
    }
    recurse(obj,'');

    return result;
}


console.log(flatten({a:0,b:{a:1}}));